package com.yuecai.modules.activity_coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuecai.modules.activity_coupon.domain.YcActivityGiveCoupon;
import com.yuecai.modules.activity_coupon.domain.dto.YcActivityGiveCouponDto;
import com.yuecai.modules.activity_coupon.domain.vo.YcActivityGiveCouponVo;
import com.yuecai.modules.activity_coupon.mapper.YcActivityGiveCouponMapper;
import com.yuecai.modules.yc_coupon.domain.YcCoupon;
import com.yuecai.modules.yc_coupon.service.YcCouponService;
import com.yuecai.modules.yc_user_coupon.domain.YcUserCoupon;
import com.yuecai.modules.yc_user_coupon.repository.YcUserCouponRepository;
import com.yuecai.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dm
 * @since 2021-01-28
 */
@Service
@RequiredArgsConstructor
public class YcActivityGiveCouponService extends ServiceImpl<YcActivityGiveCouponMapper, YcActivityGiveCoupon> implements IService<YcActivityGiveCoupon> {


    private final RedisUtils redisUtils;
    private final YcCouponService ycCouponService;
    private final YcUserCouponRepository ycUserCouponRepository;
    /**
     * 后台端
     * @param dto
     * @param page
     * @return
     */
    public List<YcActivityGiveCouponVo> queryActivityGiveCoupon(YcActivityGiveCouponDto dto, Page page) {
        List<YcActivityGiveCouponVo> list = baseMapper.findYcActivityGiveCoupon(page,dto);
        return list;
    }


    /**
     * App端
     * @param dto
     * @param aLong
     * @return
     */
    public List<YcActivityGiveCouponVo> queryActivityGiveCouponUser(YcActivityGiveCouponDto dto, Long aLong) {
        List<YcActivityGiveCouponVo> list = baseMapper.queryActivityGiveCouponUser(dto);
        ArrayList<YcActivityGiveCouponVo> ycActivityGiveCouponVos = new ArrayList<>();
        for (YcActivityGiveCouponVo vo : list) {
            boolean b = redisUtils.hHasKey("ACTIVITY_GIVE_COUPON" + vo.getId(), aLong + "");
            if (b){
                vo.setGive(true);
            }
        }

        return ycActivityGiveCouponVos;
    }

    public ResponseEntity getCouponActivity(YcActivityGiveCouponDto dto, Long aLong) throws Exception {
        boolean b = redisUtils.hHasKey("ACTIVITY_GIVE_COUPON" + dto.getId(), aLong + "");
        if (b){
            throw new Exception("您已经领取过了！");
        }
        Timestamp now = new Timestamp(System.currentTimeMillis());
        YcActivityGiveCoupon ycActivityGiveCoupon = baseMapper.selectOne(Wrappers.<YcActivityGiveCoupon>
                lambdaQuery().eq(YcActivityGiveCoupon::getId,dto.getId())
                .lt(YcActivityGiveCoupon::getEndDate,now)
                .ge(YcActivityGiveCoupon::getStartDate,now));
        ArrayList<YcUserCoupon> ycCouponByCoupons = new ArrayList<YcUserCoupon>();
        if (ycActivityGiveCoupon!=null){
            List<YcCoupon> couponByActivityID = baseMapper.findCouponByActivityID(dto.getId());
            for (YcCoupon ycCoupon : couponByActivityID) {
                boolean ycCouponByCoupon = ycCouponService.getYcCouponByCoupon(ycCoupon, aLong);
                if (ycCouponByCoupon){
                    YcUserCoupon ycUserCoupon = ycCouponService.saveYcUserCoupon(ycCoupon, aLong, 7);
                    ycCouponByCoupons.add(ycUserCoupon);
                }
            }
            if (CollectionUtils.isNotEmpty(ycCouponByCoupons)){
                ycUserCouponRepository.saveAll(ycCouponByCoupons);
                redisUtils.hincr("ACTIVITY_GIVE_COUPON" + dto.getId(), aLong + "",1);
                return ResponseEntity.ok("领取成功！");
            }else {
                throw new Exception("不符合领取条件！");
            }
        }

        throw new Exception("已经过期！");
    }
}
